﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MongoDB
{
    public partial class MasterSlave : Form
    {
        formmain fm;
        List<string> com;
        bool isHide = false;
        public MasterSlave(formmain _fm, List<string> _com)
        {
            fm = _fm;
            com = _com;
            InitializeComponent();
        }

        private void MasterSlave_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isHide)
            {
                fm.Close();
            }
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            isHide = true;
            fm.Show();
            this.Close();
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            if (chkmaster.Checked)
            {
                com.Add("master=true");
            }
            else
            {
                com.Add("slave=true");
                com.Add(string.Format("source={0}:{1}",txthost.Text,txtport.Text));
            }
            var filepath = string.Format(@"{0}\mongodb.cfg", Application.StartupPath);
            File.Delete(filepath);
            File.AppendAllLines(filepath, com, Encoding.ASCII);
            InstallServer iis = new InstallServer(fm, filepath, this);
            iis.Show();
            this.Hide();
        }

        private void chkmaster_CheckedChanged(object sender, EventArgs e)
        {
            if (chkmaster.Checked)
            {
                txthost.Enabled = false;
                txtport.Enabled = false;
                labmaster.Enabled = false;
            }
            else
            {
                txthost.Enabled = true;
                txtport.Enabled = true;
                labmaster.Enabled = true;
            }
            btnnext.Enabled = true;
        }
    }
}
